/************************************************************************************/
/*  Program name CAPAPIMP_AP_OPT.sql                                                */
/*  Creater:  WONG Wing Kin (PCCW - UniHub)                                         */
/*************   History   *************************                                */
/* 30 NOV, 2005   WONG Wing Kin         First created                               */
/************************************************************************************/
SET SERVEROUTPUT ON SIZE 1000000;

DECLARE
	lv_Prog_ID		VARCHAR2(100)	:=	'CAPAPIMP_AP_OPT';

	lv_Scc_Count	NUMBER			:=	0;
	lv_Err_Count	NUMBER			:=	0;
	lv_Err_Msg		VARCHAR2(500)	:=	'No Error';

	CURSOR Cur_F IS
	SELECT
		*
	FROM
		CRST_AP_SYSTEM_PARAM;

	lv_COA_ID			NUMBER;

BEGIN
	FOR C1 IN Cur_F LOOP
		BEGIN

			lv_COA_ID	:=	CAPBRIMP_COMMON.GetCOAID(C1.SOB_SHORT_NAME);

			INSERT INTO AP_SYSTEM_PARAMETERS_ALL
			VALUES
			(
			C1.LAST_UPDATE_DATE,
			CAPBRIMP_COMMON.GetUser(C1.LAST_UPDATED_BY_NAME),		--LAST_UPDATED_BY,
			CAPBRIMP_COMMON.GetSOBID(C1.SOB_SHORT_NAME),			--SET_OF_BOOKS_ID,
			C1.BASE_CURRENCY_CODE,
			C1.RECALC_PAY_SCHEDULE_FLAG,
			C1.AUTO_CALCULATE_INTEREST_FLAG,
			C1.INVOICE_CURRENCY_CODE,
			C1.PAYMENT_CURRENCY_CODE,
			C1.INVOICE_NET_GROSS_FLAG,
			C1.BANK_ACCOUNT_ID,										--  ???
			C1.CHECK_OVERFLOW_LOOKUP_CODE,
			C1.BATCH_CONTROL_FLAG,
			CAPBRIMP_COMMON.GetAPTerm(C1.AP_TERM_NAME),				--TERMS_ID
			C1.ALWAYS_TAKE_DISC_FLAG,
			C1.PAY_DATE_BASIS_LOOKUP_CODE,
			CAPBRIMP_COMMON.GetSegment(lv_COA_ID,C1.ACCTS_PAY_CCID_D),		--ACCTS_PAY_CODE_COMBINATION_ID,
			CAPBRIMP_COMMON.GetSegment(lv_COA_ID,C1.SALES_TAX_CCID_D),		--SALES_TAX_CODE_COMBINATION_ID,
			CAPBRIMP_COMMON.GetSegment(lv_COA_ID,C1.DISC_LOST_CCID_D),		--DISC_LOST_CODE_COMBINATION_ID,
			CAPBRIMP_COMMON.GetSegment(lv_COA_ID,C1.DISC_TAKEN_CCID_D),		--DISC_TAKEN_CODE_COMBINATION_ID,
			CAPBRIMP_COMMON.GetSegment(lv_COA_ID,C1.HOLD_GAIN_CCID_D),		--HOLD_GAIN_CODE_COMBINATION_ID,
			CAPBRIMP_COMMON.GetSegment(lv_COA_ID,C1.TRANS_GAIN_CCID_D),		--TRANS_GAIN_CODE_COMBINATION_ID,
			C1.APPLY_ADVANCES_DEFAULT,
			C1.ADD_DAYS_SETTLEMENT_DATE,
			C1.COST_OF_MONEY,
			C1.DAYS_BETWEEN_CHECK_CYCLES,
			C1.FEDERAL_IDENTIFICATION_NUM,
			CAPBRIMP_COMMON.GetLocation(C1.LOCATION_NAME),					--LOCATION_ID,
			C1.CREATE_EMPLOYEE_VENDOR_FLAG,
			CAPBRIMP_COMMON.GetAPTerm(C1.EMPLOYEE_TERM_NAME),				--EMPLOYEE_TERMS_ID,
			C1.EMPLOYEE_PAY_GROUP_LOOKUP_CODE,
			C1.EMPLOYEE_PAYMENT_PRIORITY,
			CAPBRIMP_COMMON.GetSegment(lv_COA_ID,C1.PREPAY_CCID_D),			--PREPAY_CODE_COMBINATION_ID,
			C1.CONFIRM_DATE_AS_INV_NUM_FLAG,
			C1.UPDATE_PAY_SITE_FLAG,
			C1.DEFAULT_EXCHANGE_RATE_TYPE,
			CAPBRIMP_COMMON.GetSegment(lv_COA_ID,C1.GAIN_CCID_D),			--GAIN_CODE_COMBINATION_ID,
			CAPBRIMP_COMMON.GetSegment(lv_COA_ID,C1.LOSS_CCID_D),			--LOSS_CODE_COMBINATION_ID,
			C1.MAKE_RATE_MANDATORY_FLAG,
			C1.MULTI_CURRENCY_FLAG,
			C1.GL_DATE_FROM_RECEIPT_FLAG,
			C1.DISC_IS_INV_LESS_TAX_FLAG,
			C1.MATCH_ON_TAX_FLAG,
			C1.ACCOUNTING_METHOD_OPTION,
			C1.EXPENSE_POST_OPTION,
			C1.DISCOUNT_TAKEN_POST_OPTION,
			C1.GAIN_LOSS_POST_OPTION,
			C1.CASH_POST_OPTION,
			C1.FUTURE_PAY_POST_OPTION,
			C1.DATE_FORMAT_LOOKUP_CODE,
			C1.REPLACE_CHECK_FLAG,
			C1.ONLINE_PRINT_FLAG,
			C1.EFT_USER_NUMBER,
			C1.MAX_OUTLAY,
			C1.VENDOR_PAY_GROUP_LOOKUP_CODE,
			C1.REQUIRE_TAX_ENTRY_FLAG,
			C1.APPROVALS_OPTION,
			C1.POST_DATED_PAYMENTS_FLAG,
			C1.SECONDARY_ACCOUNTING_METHOD,
			C1.SECONDARY_SET_OF_BOOKS_ID,			--  ???
			C1.TAKE_VAT_BEFORE_DISCOUNT_FLAG,
			C1.INTEREST_TOLERANCE_AMOUNT,
			CAPBRIMP_COMMON.GetSegment(lv_COA_ID,C1.INTEREST_CCID_D),				--INTEREST_CODE_COMBINATION_ID,
			C1.TERMS_DATE_BASIS,
			C1.ALLOW_FUTURE_PAY_FLAG,
			C1.AUTO_TAX_CALC_FLAG,
			C1.AUTOMATIC_OFFSETS_FLAG,
			C1.LIABILITY_POST_LOOKUP_CODE,
			CAPBRIMP_COMMON.GetSegment(lv_COA_ID,C1.INTEREST_ACCTS_PAY_CCID_D),	--INTEREST_ACCTS_PAY_CCID,
			C1.LIABILITY_POST_OPTION,
			C1.DISCOUNT_DISTRIBUTION_METHOD,
			CAPBRIMP_COMMON.GetSegment(lv_COA_ID,C1.RATE_VAR_CCID_D),			--RATE_VAR_CODE_COMBINATION_ID,
			C1.COMBINED_FILING_FLAG,
			C1.INCOME_TAX_REGION,
			C1.INCOME_TAX_REGION_FLAG,
			C1.HOLD_UNMATCHED_INVOICES_FLAG,
			C1.ALLOW_DIST_MATCH_FLAG,
			C1.ALLOW_FINAL_MATCH_FLAG,
			C1.ALLOW_FLEX_OVERRIDE_FLAG,
			C1.ALLOW_PAID_INVOICE_ADJUST,
			C1.USSGL_TRANSACTION_CODE,
			C1.USSGL_TRX_CODE_CONTEXT,
			C1.INV_DOC_CATEGORY_OVERRIDE,
			C1.PAY_DOC_CATEGORY_OVERRIDE,
			C1.VENDOR_AUTO_INT_DEFAULT,
			C1.SUMMARY_JOURNALS_DEFAULT,
			CAPBRIMP_COMMON.GetSegment(lv_COA_ID,C1.RATE_VAR_GAIN_CCID_D),		--RATE_VAR_GAIN_CCID,
			CAPBRIMP_COMMON.GetSegment(lv_COA_ID,C1.RATE_VAR_LOSS_CCID_D),		--RATE_VAR_LOSS_CCID,
			C1.TRANSFER_DESC_FLEX_FLAG,
			C1.ALLOW_AWT_FLAG,
			C1.DEFAULT_AWT_GROUP_ID,			--  ???
			C1.ALLOW_AWT_OVERRIDE,
			C1.CREATE_AWT_DISTS_TYPE,
			C1.CREATE_AWT_INVOICES_TYPE,
			C1.AWT_INCLUDE_DISCOUNT_AMT,
			C1.AWT_INCLUDE_TAX_AMT,
			CAPBRIMP_COMMON.GetOrgID(C1.ORG_NAME),					--ORG_ID,
			C1.RECON_ACCOUNTING_FLAG,
			C1.AUTO_CREATE_FREIGHT_FLAG,
			CAPBRIMP_COMMON.GetSegment(lv_COA_ID,C1.FREIGHT_CCID_D),				--FREIGHT_CODE_COMBINATION_ID,
			C1.GLOBAL_ATTRIBUTE_CATEGORY,
			C1.GLOBAL_ATTRIBUTE1,
			C1.GLOBAL_ATTRIBUTE2,
			C1.GLOBAL_ATTRIBUTE3,
			C1.GLOBAL_ATTRIBUTE4,
			C1.GLOBAL_ATTRIBUTE5,
			C1.GLOBAL_ATTRIBUTE6,
			C1.GLOBAL_ATTRIBUTE7,
			C1.GLOBAL_ATTRIBUTE8,
			C1.GLOBAL_ATTRIBUTE9,
			C1.GLOBAL_ATTRIBUTE10,
			C1.GLOBAL_ATTRIBUTE11,
			C1.GLOBAL_ATTRIBUTE12,
			C1.GLOBAL_ATTRIBUTE13,
			C1.GLOBAL_ATTRIBUTE14,
			C1.GLOBAL_ATTRIBUTE15,
			C1.GLOBAL_ATTRIBUTE16,
			C1.GLOBAL_ATTRIBUTE17,
			C1.GLOBAL_ATTRIBUTE18,
			C1.GLOBAL_ATTRIBUTE19,
			C1.GLOBAL_ATTRIBUTE20,
			C1.ALLOW_SUPPLIER_BANK_OVERRIDE,
			C1.USE_MULTIPLE_SUPPLIER_BANKS,
			C1.AUTO_TAX_CALC_OVERRIDE,
			C1.AMOUNT_INCLUDES_TAX_FLAG,
			C1.AMOUNT_INCLUDES_TAX_OVERRIDE,
			C1.VAT_CODE,
			C1.USE_BANK_CHARGE_FLAG,
			C1.BANK_CHARGE_BEARER,
			CAPBRIMP_COMMON.GetSegment(lv_COA_ID,C1.ROUNDING_ERROR_CCID_D),		--ROUNDING_ERROR_CCID,
			C1.ROUNDING_ERROR_POST_OPTION,
			C1.TAX_FROM_PO_FLAG,
			C1.TAX_FROM_VENDOR_SITE_FLAG,
			C1.TAX_FROM_VENDOR_FLAG,
			C1.TAX_FROM_ACCOUNT_FLAG,
			C1.TAX_FROM_SYSTEM_FLAG,
			C1.TAX_FROM_INV_HEADER_FLAG,
			C1.TAX_FROM_TEMPLATE_FLAG,
			C1.TAX_HIER_PO_SHIPMENT,
			C1.TAX_HIER_VENDOR,
			C1.TAX_HIER_VENDOR_SITE,
			C1.TAX_HIER_ACCOUNT,
			C1.TAX_HIER_SYSTEM,
			C1.TAX_HIER_INVOICE,
			C1.TAX_HIER_TEMPLATE,
			C1.ENFORCE_TAX_FROM_ACCOUNT,
			C1.MRC_BASE_CURRENCY_CODE,
			C1.MRC_SECONDARY_SET_OF_BOOKS_ID,
			C1.MATCH_OPTION,
			C1.GAIN_LOSS_CALC_LEVEL,
			C1.WHEN_TO_ACCOUNT_PMT,
			C1.WHEN_TO_ACCOUNT_GAIN_LOSS,
			C1.FUTURE_DATED_PMT_ACCT_SOURCE,
			C1.FUTURE_DATED_PMT_LIAB_RELIEF,
			C1.GL_TRANSFER_ALLOW_OVERRIDE,
			C1.GL_TRANSFER_PROCESS_DAYS,
			C1.GL_TRANSFER_MODE,
			C1.GL_TRANSFER_SUBMIT_JOURNAL_IMP,
			C1.INCLUDE_REPORTING_SOB,
			C1.EXPENSE_REPORT_ID,
			CAPBRIMP_COMMON.GetAPTerm(C1.PREPAYMENT_TERM_NAME),				--PREPAYMENT_TERMS_ID,
			C1.CALC_USER_XRATE,
			C1.SORT_BY_ALTERNATE_FIELD,
			C1.APPROVAL_WORKFLOW_FLAG,
			C1.ALLOW_FORCE_APPROVAL_FLAG,
			C1.VALIDATE_BEFORE_APPROVAL_FLAG,
			C1.XML_PAYMENTS_AUTO_CONFIRM_FLAG,
			C1.PRORATE_INT_INV_ACROSS_DISTS,
			C1.BUILD_PREPAYMENT_ACCOUNTS_FLAG,
			C1.ENABLE_1099_ON_AWT_FLAG,
			C1.STOP_PREPAY_ACROSS_BAL_FLAG,
			C1.AUTOMATIC_OFFSETS_CHANGE_FLAG
			);
		EXCEPTION
			WHEN OTHERS THEN
				DBMS_OUTPUT.Put_Line(SQLERRM);
				lv_Err_Count	:=	lv_Err_Count + 1;
		END;
		lv_Scc_Count	:=	lv_Scc_Count	+	1;
	END LOOP;

	IF	lv_Err_Count	>	0	THEN
		ROLLBACK;
	END IF;

	INSERT INTO CAPCNV_LOG(
		PROG_ID,
		PROCESS_DATE,
		RECORD_PROCESSED,
		RECORD_REJECTED,
		RECORD_EXTRACTED,
		ERR_MSG
	) VALUES (
		lv_Prog_ID,
		SYSDATE,
		lv_Scc_Count,
		lv_Err_Count,
		lv_Scc_Count-lv_Err_Count,
		lv_Err_Msg
	);

	COMMIT;
END;
/
